package com.example.beijing.beijingdemo.base;


import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.beijing.beijingdemo.R;
import com.example.beijing.beijingdemo.activity.MainActivity;

/**
 * 5个TabFragment页面的基类
 * 相同的行为:
 * 1.加载布局
 * 2.设置内容
 */

public abstract class BaseFragment extends Fragment implements View.OnClickListener {

    private ImageButton mButtonLeft;
    private TextView mTvTitle;
    private ImageButton mButtonRight;
    private FrameLayout mContainer;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab_base, container, false);

        mButtonLeft = (ImageButton) view.findViewById(R.id.fragment_base_menu);
        mTvTitle = (TextView) view.findViewById(R.id.fragment_base_title);
        mButtonRight = (ImageButton) view.findViewById(R.id.fragment_base_grid);
        mContainer = (FrameLayout) view.findViewById(R.id.fragment_base_container);

        mButtonLeft.setOnClickListener(this);

        return view;
    }

    // 布局创建完成
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        intiTitle();
    }

    /**
     * 初始化标题,让子类去实现
     */
    public abstract void intiTitle();

    /**
     * 设置标题内容
     */
    public void setTitle(String title) {
        mTvTitle.setText(title);
    }

    /**
     * 是否显示左边的图标
     */
    public void setIpMenuDisplayState(boolean isShow) {
        mButtonLeft.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    /**
     * 是否显示右边的图标
     */
    public void setIpPicDisplayState(boolean isShow) {
        mButtonRight.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    /**
     * 想容器里面添加内容
     */
    public void addView(View view) {
        // 先清空内容
        mContainer.removeAllViews();

        // 添加
        mContainer.addView(view);
    }

    /**
     * 创建内容的View,让子类实现
     */
    public abstract View createContent();

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fragment_base_menu:
                ((MainActivity) getActivity()).slidingMenu.toggle();
                break;
            case R.id.fragment_base_grid:

                break;
        }
    }
}